<div class="buying-services">
    <h3 class="popup-title global-warning">Вызов мастера на дом</h3>
    <div class="buying-services__form">
        <?php
        $form = $this->beginWidget('CActiveForm', array(
            'id' => 'buying-form-2',
            'action' => '/main/ajax/repairer',
            'enableClientValidation' => true,
            'clientOptions' => array(),
            'htmlOptions' => array('class' => 'buy__service__ajax_form'),
        ));
        ?>
        <div style="display: none">
            <?php echo $form->error($model, 'userName'); ?>
            <?php echo $form->error($model, 'userPhone'); ?>
        </div>
        <fieldset>
            <div class="type-text">
                <?php echo $form->textField($model, 'userName', array('placeholder' => 'Ваше имя')); ?>	
            </div>
        </fieldset>
        <fieldset>
            <div class="type-text">
                <?php echo $form->textField($model, 'userPhone', array('placeholder' => 'Ваш телефон', 'class' => 'masked-phone', 'id' => 'RepairerForm_userPhone_2')); ?>				
            </div>
        </fieldset>
        <fieldset style="display: block;">
            <div class="type-text">
                <?php echo $form->textField($model, 'userEmail', array('placeholder' => 'Ваш E-mail')); ?>	
            </div>
        </fieldset>
        <fieldset>
            <div class="textarea">
                <?php echo $form->textArea($model, 'userMsg', array('placeholder' => 'Комментарий')); ?>	
            </div>
        </fieldset>
        <fieldset style="display: none;">
            <div class="select">
                <?php
                $model->city = Yii::app()->params['regions'][Yii::app()->session['userRegion']['id']]['regionTitle'];
                $uRegions = [];
                foreach (Yii::app()->params['regions'] as $region) {
                    $uRegions[$region['regionTitle']] = $region['regionTitle'];
                }
                ?>
                <?php echo $form->dropdownList($model, 'city', $uRegions); ?>
            </div>
        </fieldset>

        <fieldset>
            <div class="type-submit">
                <?php echo CHtml::submitButton('Заказать'); ?>				
            </div> 
        </fieldset>
        <?php $this->endWidget(); ?>
    </div>	
    <p class="global-warning">После отправки формы, наши менеджеры свяжутся с вами в течение 15 рабочих минут!</p>
</div>
<script type="text/javascript">
    $("#RepairerForm_userPhone_2").mask("+7 (999) 999-99-99");
    $('#buying-form-2').yiiactiveform(
            {
                'validateOnSubmit': true,
                'afterValidate': function (form, data, hasError)
                {
                    if (!hasError)
                    {
                        console.log(form);
                        
                        mobi03.sendGoogleStatic("call-master", $('#RepairerForm_userPhone_2').val(), window.location.href);

                        $.ajax({
                            type: "POST",
                            url: form[0].action,
                            data: $(form).serialize(),
                            success: function (ret)
                            {
                                console.log(data);
                                $('.fancybox-close').trigger('click');
                                var data = JSON.parse(ret);
                                mobi03.showMessage(data.message.title, data.message.desc);
                            }
                        });
                    }
                },
                'attributes': [{
                        'id': 'RepairerForm_userName',
                        'inputID': 'RepairerForm_userName',
                        'errorID': 'RepairerForm_userName_em_',
                        'model': 'RepairerForm',
                        'name': 'userName',
                        'enableAjaxValidation': false,
                        'clientValidation': function (value, messages, attribute)
                        {
                            if (jQuery.trim(value) == '')
                            {
                                messages.push("\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u043e\u043b\u0435 \u00abUser Name\u00bb.");
                            }
                        }
                    },
                    {'id': 'RepairerForm_userPhone_2',
                        'inputID': 'RepairerForm_userPhone_2',
                        'errorID': 'RepairerForm_userPhone_2_em_',
                        'model': 'RepairerForm',
                        'name': 'userPhone',
                        'enableAjaxValidation': false,
                        'clientValidation': function (value, messages, attribute)
                        {
                            if (jQuery.trim(value) == '')
                            {
                                messages.push("\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u043e\u043b\u0435 \u00abUser Phone\u00bb.");
                            }

                        }
                    },
                    {'id': 'RepairerForm_userAddress',
                        'inputID': 'RepairerForm_userAddress',
                        'errorID': 'RepairerForm_userAddress_em_',
                        'model': 'RepairerForm',
                        'name': 'userPhone',
                        'enableAjaxValidation': false,
                        'clientValidation': function (value, messages, attribute)
                        {
                            if (jQuery.trim(value) == '' && $("form#buying-form div.checkbox").hasClass("open"))
                            {
                                messages.push("\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u043e\u043b\u0435 \u00abUser Address\u00bb.");
                            }

                        }
                    }
                ],
                'errorCss': 'error'
            });
</script>